package com.ethannhu.todaynews.activity

import android.view.MenuItem
import androidx.appcompat.widget.Toolbar
import androidx.viewbinding.ViewBinding
import com.ethannhu.todaynews.R


/**
 * 通用标题界面
 */
open class BaseTitleActivity<VB : ViewBinding> : BaseViewModelActivity<VB>() {
    private lateinit var toolbar: Toolbar

    override fun initViews() {
        super.initViews()
        toolbar = findViewById(R.id.toolbar)

        //初始化 Toolbar
        setSupportActionBar(toolbar)

        //是否显示返回按钮
        if (isShowBackMenu()) {
            showBackMenu()
        }
    }

    protected fun isShowBackMenu(): Boolean {
        return true
    }

    protected fun showBackMenu() {
        supportActionBar!!.setDisplayHomeAsUpEnabled(true)
    }


    //菜单点击回调
    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            android.R.id.home ->
                finish()
        }
        return super.onOptionsItemSelected(item)
    }

}